package com.yc.RSS.buffer;

import com.yc.RSS.CommonInformationItem;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author zwei
 * @create 2024-07-24 15:04
 */
public class NewsBuffer {
    //用于存新闻的阻塞队列
    private LinkedBlockingQueue<CommonInformationItem>buffer;
    //用于去重的一个map
    private ConcurrentHashMap<String,String> storedItem;        //已经保存的新闻的id
    public NewsBuffer(){
        buffer=new LinkedBlockingQueue<>();
        storedItem=new ConcurrentHashMap<>();
    }
    public void add(CommonInformationItem item){
        //去重
        if (storedItem.containsKey(item.getId())){
            return;
        }
        buffer.add(item);
        storedItem.put(item.getId(),item.getId());
        //TODO:大数据保存方式？ hash算法->位图->布隆过滤器
    }
    public CommonInformationItem get() throws InterruptedException {
        //从阻塞队列中获取一个item
        return buffer.take();
    }
}
